<?php
class ProductsController extends Zend_Controller_Action
{
    protected $_categories;
    
    public function init()
    {
        parent::init();
        $this->_model = new Application_Model_DbTable_Product();
//        $this->_categories = new Category();
    }
    
    
    public function indexAction()
    {
        $this->view->page_title = '产品中心';

        $products = new Application_Model_DbTable_Product();
        $paginator = $products->getProducts(
                $this->_getParam('page', 1),
                array('sort_id')
        );
        $this->view->products = $paginator;
        $this->view->paginator = $paginator;
//        $this->view->pager = $this->view->paginate($rowset,
//            $this->_getAllParams())->getPager();

        Zend_Paginator::setDefaultScrollingStyle('Sliding');
        Zend_View_Helper_PaginationControl::setDefaultViewPartial(
            'pagination_control.tpl.htm'
        );
    }
    
    
    public function detailAction()
    {
        $id = $this->_getId();
        $product = $this->_model->fetchRow("id='$id'");
        $product->description = stripslashes($product->description);
        
        $this->view->product = $product->toArray();
        $this->_breadcrumbAndSubcatgs($product->category_id, $product->name);
        $this->view->page_title = $product->name;
    }
    
    
    public function searchAction()
    {
        $this->view->page_title = '搜索结果';
        
        $products = new Riskle_Db_Table_Paginate($this->_model, 
            $this->_getParam('page'), 16);
        $searchId = $this->_request->getQuery('search_id');
        if ($searchId === '') {
            $searchIdCondition = '';
        } else {
            $searchIdCondition = "categories.search_id like '$searchId%' and ";
        }
        $key = trim($this->_request->getQuery('key'));
        if ($key === '') {
            $this->_goBack('请输入查询关键词！');
        }
        if (mb_strlen($key, "UTF-8") < 2) {
            $this->_goBack('查询关键词过短！');
        }
        $where = "
            $searchIdCondition
            (
                products.description like '%$key%'
                or 
                products.name like '%$key%'
            )
        ";

        $rowset = $products->fetchAll($where);
        
        $breadcrumb = array();
        $breadcrumb['top'] = '搜索结果';
        $breadcrumb['full'] = '搜索结果';
        $this->view->breadcrumb = $breadcrumb;
        
        $this->view->products = $rowset->toArray();
        $this->view->pager = $this->view->paginate($rowset, 
            $this->_getAllParams())->getPager();
        $this->render('index');
    }
    
    
    public function categoryAction()
    {
        $this->view->page_title = '产品中心';
        
        $id = $this->_getId();
        $this->_breadcrumbAndSubcatgs($id);
        
        $searchId = sprintf("%04s", $id);
        $where = "categories.search_id like '$searchId%'";
        
        $products = new Riskle_Db_Table_Paginate($this->_model, 
            $this->_getParam('page'), 16);       
        $rowset = $products->fetchAll($where, 'sort_id DESC');
        $this->view->products = $rowset->toArray();
        $this->view->pager = $this->view->paginate($rowset, 
            $this->_getAllParams())->getPager(); 

        $this->render('index');
    }
    
    
    public function subcategoryAction()
    {
        $products = new Riskle_Db_Table_Paginate($this->_model, 
            $this->_getParam('page'), 16);
        $id = $this->_getId();
        $breadcrumb = $this->_breadcrumbAndSubcatgs($id);
        $rowset = $products->fetchAll("products.category_id='$id'", "sort_id DESC");

        $this->view->page_title = $breadcrumb['full'] . ' &nbsp;&nbsp;- 产品中心';
        $this->view->products = $rowset->toArray();
        $this->view->pager = $this->view->paginate($rowset, 
            $this->_getAllParams())->getPager();
        $this->render('index');
    }
    
    
    protected function _breadcrumbAndSubcatgs($id, $productName = '')
    {
        $ancestors = $this->_categories->getAncestors($id);

        $fullPath = '';
        foreach ($ancestors as $ancestor) {
            $fullPath .= "{$ancestor->name}/ ";
        }
        if ($productName !== '') {
            $fullPath .= $productName;
        }
        
        $breadcrumb = array(
            'top'   => $ancestors[0]->name,
            'full'  => $fullPath
        );
        $this->view->breadcrumb = $breadcrumb;
        
        return $breadcrumb;
    }
}